/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.mvp;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.HasData;
import redora.client.Persistable;
import redora.client.constants.RedoraConstants;
import redora.client.util.Field;

import java.util.Arrays;

import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_RIGHT;

/**
 * TableView implementation. Shows a scrollable table with all (FindAll) record
 * of given pojo. You can look at the generated code and find something like this:
 * <code>
        util = clientFactory.getLocator().locateUtil(cls);
        display = new TableViewImpl(cls, clientFactory.getLocator());
        //.... do stuff
        display.initTable(); //Before this you can play with the display.fields.
 * </code>
 * If you create your own activity you can change the look of this view in the 'do stuff'
 * part.<br>
 * The table view will show all table scoped attributes of the pojo.
 *
 * @see BrowseActivity
 * @author Nanjing RedOrange (www.red-orange.cn)
 */
public class TableViewImpl extends FieldedComposite implements TableView {

    RedoraConstants redoraConstants = GWT.create(RedoraConstants.class);

    final FlowPanel panel = new FlowPanel();
    final CellTable table;

    TableView.Presenter presenter;

    final Anchor addButton;

    @Override
    public HasClickHandlers addButton() {
        return addButton;
    }

    @Override
    public HasData getData() {
        return table;
    }

    @Override
    public HasWidgets panel() {
        return panel;
    }

    /**
     * Creates a standard scroll table of given pojo. {@link #fields()} is set
     * to {@link redora.client.Fields#tableAndSetFields()}
     *
     * @param cls (Mandatory)
     * @param locator (Mandatory)
     */
    public TableViewImpl(Class<? extends Persistable> cls, Locator locator, CellTable<? extends Persistable> table) {
        this.table = table;
        table.setWidth("100%");
        // Create a Pager to control the table.
        SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
        SimplePager pager = new SimplePager(SimplePager.TextLocation.CENTER, pagerResources, false, 0, true);
        pager.setDisplay(table);
        pager.setPageSize(20);
        addButton = new Anchor(redoraConstants.add(), new EditPlace(cls, null).toString());

        HorizontalPanel actionPanel = new HorizontalPanel();
        actionPanel.setWidth("100%");
        actionPanel.add(addButton);
        actionPanel.add(pager);
        actionPanel.setCellHorizontalAlignment(pager, ALIGN_RIGHT);

        panel.add(actionPanel);
        panel.add(table);

        fields.addAll(Arrays.asList(locator.locateFields(cls).tableAndSetFields()));

        this.initWidget(panel);
    }

    /**
     * Actually creates the table and adds it to the panel. If you want to
     * change the fields, you must do so before the activity calls this method.
     */
    @Override
    public void initTable() {
        for (Field field : fields) {
            if (field.column != null) {
                table.addColumn(field.column, field.displayNameTable);
            }
        }
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public Widget asWidget() {
        return this;
    }
}